package com.music.dao;

import com.laoxuai.beans.annotation.Component;
import com.laoxuai.ibatis.support.BaseDao;
import com.music.pojo.Artist;
import com.music.pojo.Music;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

@Component
public class MusicDao extends BaseDao<Music, Integer> {
    public int selectMaxId() throws SQLException {
        String sql="select * from t_music where id=(\n" +
                 "select max(id) from t_music);";
        Music reMusic=runner.query(sql, new BeanHandler<Music>(Music.class));
        if(reMusic==null){
            return 0;
        }else {
            return reMusic.getId();
        }
    }
    public int inset_music_artist(int music_id,String artist_name) throws SQLException {
        String sql="select * from t_artist where name =?";
        Object[] par={artist_name};
        Artist artist=runner.query(sql,new BeanHandler<Artist>(Artist.class),par);
        String insql="insert into t_artist_music(artist_id,music_id) values(?,?)";
        Object[] pars={artist.getId(),music_id};
        return runner.update(insql,pars);
    }
    public List<Music> selectAll() throws SQLException {
        String sql = "select * from t_music";
        return runner.query(sql,new BeanListHandler<Music>(Music.class));
    }
}
